package snooker.scoreboard.persistence;

import snooker.scoreboard.frame.Score;
import android.content.SharedPreferences;
import android.util.Log;

public class SnookerSharedPreferences {

	private static final String LOG_TAG = "SnookerSharedPreferences";
	
	public static final String KEY_PLAYER1_NAME 	= "Player1Name";
	public static final String KEY_PLAYER2_NAME 	= "Player2Name";
	public static final String KEY_FILE_NAME 		= "filename";
	public static final String KEY_ACT_FRAME_ID 	= "actFrameId";
	public static final String KEY_NUMBER_OF_LINES  = "lineCount";
	public static final String KEY_LINE 			= "line";
	
	private static SnookerSharedPreferences ssp;
	
	private SharedPreferences sp;
	private SharedPreferences.Editor ieditor;
	
	private int index = 0;
	
	private SnookerSharedPreferences(SharedPreferences sp) {
		this.sp = sp;
		ieditor = sp.edit();
	}
	
	protected static void create(SharedPreferences sp) {
		ssp = new SnookerSharedPreferences(sp);
	}
	
	public static SnookerSharedPreferences getInstance() {
		if (ssp == null) {
			ssp = new SnookerSharedPreferences(null);
		}
		return ssp;
	}
	
	public void savePlayer1Name(String name) {
		SharedPreferences.Editor editor = sp.edit();
		editor.putString(KEY_PLAYER1_NAME, name);
		editor.commit();
	}

	public String getPlayer1Name() {
		return sp.getString(KEY_PLAYER1_NAME, "Player1");
	}
	
	public void savePlayer2Name(String name) {
		SharedPreferences.Editor editor = sp.edit();
		editor.putString(KEY_PLAYER2_NAME, name);
		editor.commit();
	}

	public String getPlayer2Name() {
		return sp.getString(KEY_PLAYER2_NAME, "Player2");
	}
	
	public void saveFileName(String name) {
		SharedPreferences.Editor editor = sp.edit();
		editor.putString(KEY_FILE_NAME, name);
		editor.commit();
	}
	
	public String getFileName() {
		return sp.getString(KEY_FILE_NAME, null);
	}
	
	public void saveActFrameId(int actFrameId) {
		SharedPreferences.Editor editor = sp.edit();
		editor.putInt(KEY_ACT_FRAME_ID, actFrameId);
		editor.commit();
	}
	
	public int getActiveFrameId()  {
		return sp.getInt(KEY_ACT_FRAME_ID, 1);
	}
	
	public void saveScore(Score score) {
		Log.i(LOG_TAG, "saveScore() {");
		
		int lines = sp.getInt(KEY_NUMBER_OF_LINES, 0);
		
		ieditor.putString(KEY_LINE + lines, score.getSerializedString());
		lines++;
		ieditor.putInt(KEY_NUMBER_OF_LINES, lines);
		
		ieditor.commit();
	}
	
	public void saveScore(String score) {
		ieditor.putString(KEY_LINE + index, score);
		Log.i("index",index+"");
		index++;
	}
	
	public void commit() {
		ieditor.putInt(KEY_NUMBER_OF_LINES, index);
		ieditor.commit();
	}
	
	public void removeLastScore() {
		int lines = sp.getInt(KEY_NUMBER_OF_LINES, 0);
		if (lines > 0) {
			SharedPreferences.Editor editor = sp.edit();
			lines--;
			editor.remove(KEY_LINE + lines);
			editor.putInt(KEY_NUMBER_OF_LINES, lines);
			editor.commit();
		}
	}
	
	public Score[] getScores() {
		
		Log.i(LOG_TAG, "getScores() {");
		
		int lines = sp.getInt(KEY_NUMBER_OF_LINES, 0);
		Log.i(LOG_TAG, " lines=" + lines);
		
		Score[] scoreArray = new Score[lines];
		for (int i = 0; i < lines; i++) {
			String line = sp.getString(KEY_LINE + i, "");
			Log.i(LOG_TAG, " " + i + " - " + line);
			scoreArray[i] = Score.getDeserializedScore(line);
//			Log.i(LOG_TAG, " " + i + " - " + scoreArray[i].getSerializedStringOld("fuck"));
		}
		return scoreArray;
	}
	
	public void clean() {
		
		Log.i(LOG_TAG, "clean() { ");
		
		SharedPreferences.Editor editor = sp.edit();
		int actlines = sp.getInt(KEY_NUMBER_OF_LINES, 0);
		if (actlines > 0) {
			for (int i = 0; i < actlines; i++) {
				editor.remove(KEY_LINE + i);
			}
		}
		editor.remove(KEY_NUMBER_OF_LINES);
		editor.remove(KEY_ACT_FRAME_ID);
		editor.remove(KEY_FILE_NAME);
		editor.commit();
		index = 0;
	}
	
}
